package com.du.cloud.service.oauth.extend.phone;

import com.du.cloud.security.service.TokenUser;
import com.du.cloud.service.oauth.extend.AbstractCustomTokenGranter;
import com.du.cloud.service.oauth.service.CustomUserDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 * Desc:
 * Author: dushuang
 * Date: 2020/5/29
 **/
public class PhoneSmsTokenGranter extends AbstractCustomTokenGranter {

    private CustomUserDetailsService userDetailsService;

    public PhoneSmsTokenGranter(AuthorizationServerTokenServices tokenServices,
                                     ClientDetailsService clientDetailsService,
                                     OAuth2RequestFactory requestFactory,
                                     CustomUserDetailsService userDetailsService) {
        super(tokenServices, clientDetailsService, requestFactory,"custom_phone_sms");
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected TokenUser getUserDetails(Map<String, String> parameters) {
        String phone = parameters.get("phone");
        String smsCode = parameters.get("smsCode");
        return userDetailsService.loadUserByPhoneAndSmsCode(phone, smsCode);
    }
}
